'''
Created on 2017年10月8日

@author: Administrator
'''
import json

class Student(object):
    def __init__(self,name,age,score):
        self.name=name
        self.age=age
        self.score = score

def student2dict(std):
    return {
        'name': std.name,
        'age': std.age,
        'score': std.score
    }
def dict2student1(std):
    return Student(std['name'],std['age'],std['score'])    
if __name__ == '__main__':
    d = dict(name='Bob', age=20, score=88)
    data=json.dumps(d)
    print("序列化后",data)
    c=json.loads(data)
    print("解析后：",c)
    
    s = Student('Bob', 20, 88)
    data=json.dumps(d,default=student2dict)
    print("对象序列化后:",data)
    
    print("对象序列化2:",json.dumps(s, default=lambda obj: obj.__dict__))
    st=json.loads(data,object_hook=dict2student1)
    print("解析后的对象：",st)
    
    